package com.num2.controller.lhr;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.num2.form.lhr.NextPage;
import com.num2.form.lhr.OpenMoneyForm;
import com.num2.form.lhr.UserForm;
import com.num2.service.lhr.OpenAccService;


@Controller
@RequestMapping("/openAccout")
public class OpenAccount {
	//服务层
	@Resource
	private OpenAccService openacc;
	
	@RequestMapping(value="/openAcc",method=RequestMethod.POST)
	@ResponseBody
	public String openAcc(@RequestParam String jso) throws JsonParseException, JsonMappingException, IOException{
		//publci String openAcc(@RequestBody UserForm uf){
		ObjectMapper mapper = new ObjectMapper();
		UserForm uf=mapper.readValue(jso, UserForm.class);
		String str=null;	
		if(openacc.openSelec(uf.getUser_name())){	
		if(openacc.openAcc(uf)){
			str="开户成功！！";
		}else{
			str="开户失败！！";
		}
		}else{
			str="开户失败！！";
		}
		return str;
	}
	//查询用户名是否可用
	@RequestMapping("/openSelc")
	@ResponseBody
	public String accountSelc(@RequestParam String usename){
		String selresult=null;
		if(openacc.openSelec(usename)){
			selresult="该用户可用";
		}else{
			selresult="用户名重复，请重新输入";
		}
		return selresult;
	}
	@RequestMapping("/openDeposit")
	@ResponseBody
	public String opendeposit(int openMoney,String usernum,OpenMoneyForm omf){	
		Date now=new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String depositdate = dateFormat.format(now);
		int useid=openacc.openuseidSel(usernum);
		omf.setBalanace(openMoney);
		omf.setUseid(useid);
		omf.setDepodate(depositdate);
		if(openacc.deposOpenMoney(omf)){
			return "开户成功";
		}else{
		return "开户失败";
		}
	}
	//获取全部用户
	@RequestMapping("/allSelectCon")
	public String allSelectCon(HttpServletRequest request,int currentPage){
		NextPage result = openacc.allSelectServ(currentPage);
		//List<allSelModel> results = result.getResPage();
		request.setAttribute("resultall", result);
		return "demo/allAccount";		
	}
	//获取启用用户
	@RequestMapping("/enableSelectCon")
	public String enableSelectCon(HttpServletRequest request,int currentPage){
		NextPage result = openacc.enableSelectServ(currentPage);
		request.setAttribute("enableResult", result);
		return "demo/enableAccount";		
	}	
	//获取冻结用户
	@RequestMapping("/frozeSelectCon")
	public String frozeSelectCon(HttpServletRequest request,int currentPage){
		NextPage result = openacc.frozeSelectServ(currentPage);
		request.setAttribute("frozeResult", result);
		return "demo/freezeAccount";		
	}	
	
	//将用户冻结
	@RequestMapping("/closeAccCon")
	@ResponseBody
	public String closeAcc(String usernum){
		String close=null;
		if(openacc.closeAccSel(usernum)){
			close="该用户已成功被冻结";
		}else{
			close="冻结用户失败";
		}
		return close;		
	}	
	
	//将用户开启
	@RequestMapping("/openAccCon")
	@ResponseBody
	public String openAccCon(String usernum){
		String open=null;
		if(openacc.openAccSel(usernum)){
			open="该用户已成功开启";
		}else{
			open="该用户开启失败";
		}
		return open;		
	}	
	@RequestMapping("/condition")
	@ResponseBody
	public String condition(HttpServletRequest request,int currentPage,String query){
		NextPage result = openacc.condition(currentPage, query);
		request.setAttribute("conditionResult", result);		
		return "";
	}
	public OpenAccService getOpenacc() {
		return openacc;
	}
	public void setOpenacc(OpenAccService openacc) {
		this.openacc = openacc;
	}
	

	
	
}
